/* 
 * File:	main.c
 * Authors: Johan Dahlberg <S102591>
 *				Daniel Jansson <S102496>
 * ---------------------------------
 * Task 1: Graycode
 */
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

/* Prototypes */

/*
 * Function: printGrayCode
 * Usage: printGrayCode(prefix, nBits)
 * -----------------
 * Handle the straight path
 */
void printGrayCode(string prefix, int nBits);


/*
 * Function: printYargCode
 * Usage: printYargCode(prefix, nBits)
 * -----------------
 * Handle the reverse path
 */
void printYargCode(string prefix, int nBits);

/* Codes */

main() {
	int nBits;
	string prefix = "";
	
	printf("Enter a number to generate possible graycodes: ");
	nBits = GetInteger();
	
	printGrayCode(prefix, nBits);

	getchar();
}

void printGrayCode(string prefix, int nBits) {
	if(nBits == 0) {
		printf("%s\n", prefix);
	} else {
		printGrayCode(Concat(prefix, "0"), nBits - 1);
		printYargCode(Concat(prefix, "1"), nBits - 1);
	}
}

void printYargCode(string prefix, int nBits) {
	if(nBits == 0) {
		printf("%s\n", prefix);
	} else {
		printGrayCode(Concat(prefix, "1"), nBits - 1);
		printYargCode(Concat(prefix, "0"), nBits - 1);
	}
}